<template>
  <div class="home-page">
    <div class="hero-section" v-if="!selectedArticle">
      <div class="profile-card">
        <div class="profile-header">
          <div class="profile-avatar">
            <img src="https://img1.baidu.com/it/u=2997421499,1730336498&fm=253&fmt=auto&app=138&f=JPEG?w=514&h=500" alt="头像" class="avatar-img">
          </div>
          <h1 class="profile-name">{{ profile.name }}</h1>
          <p class="profile-title">{{ profile.title }}</p>
          <p class="profile-description">{{ profile.description }}</p>
        </div>
        <div class="social-links">
          <a v-for="social in profile.socials" 
             :key="social.icon" 
             :href="social.link" 
             target="_blank" 
             class="social-link">
            <i :class="social.icon"></i>
          </a>
        </div>
      </div>
    </div>

    <div class="featured-section">
      <h2 class="section-title" v-if="!selectedArticle">最新文章</h2>
      <transition name="fade" mode="out-in">
        <div v-if="!selectedArticle" class="articles-grid">
          <div class="blog-card" 
               v-for="post in posts" 
               :key="post.id"
               @click="showArticleDetail(post)">
            <div class="card-img" :style="{ background: post.color }"></div>
            <div class="card-content">
              <span class="card-tag">{{ post.tag }}</span>
              <h3 class="card-title">{{ post.title }}</h3>
              <p class="card-excerpt">{{ post.excerpt }}</p>
              <div class="card-meta">
                <span><i class="far fa-calendar"></i> {{ post.date }}</span>
                <span><i class="far fa-clock"></i> {{ post.readTime }} 分钟阅读</span>
              </div>
            </div>
          </div>
        </div>
        <article-detail
          v-else
          :article="selectedArticle"
          @back="selectedArticle = null"
          @like="handleLike"
        />
      </transition>
    </div>
  </div>
</template>

<script>
import ArticleDetail from './ArticleDetail.vue'

export default {
  name: 'Home',
  components: {
    ArticleDetail
  },
  props: {
    profile: {
      type: Object,
      required: true
    },
    posts: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      selectedArticle: null
    }
  },
  methods: {
    handlePostClick(post) {
      this.$emit('post-click', post)
    },
    showArticleDetail(post) {
      // 模拟获取文章详细内容
      const articleWithContent = {
        ...post,
        content: `
          <h2>${post.title}</h2>
          <p>${post.excerpt}</p>
          <h3>文章内容</h3>
          <p>这是一篇关于${post.tag}的详细文章。在这里，我们将深入探讨相关主题，分享实用的技巧和最佳实践。</p>
          <h3>主要内容</h3>
          <p>文章将包含以下主要内容：</p>
          <ul>
            <li>核心概念解析</li>
            <li>实际应用案例</li>
            <li>最佳实践建议</li>
            <li>常见问题解答</li>
          </ul>
          <h3>代码示例</h3>
          <pre><code>// 示例代码
function example() {
  console.log('Hello, World!');
}</code></pre>
          <p>更多详细内容将在后续更新...</p>
        `,
        tags: [post.tag, '教程', '技术分享', '最佳实践'],
        likes: Math.floor(Math.random() * 100)
      }
      this.selectedArticle = articleWithContent
    },
    handleLike(articleId) {
      // 实现点赞功能
      console.log('Article liked:', articleId)
    }
  }
}
</script>

<style scoped>
.home-page {
  padding: 20px 0;
}

.hero-section {
  margin-bottom: 40px;
}

.profile-card {
  background: var(--card-bg);
  border-radius: 24px;
  padding: 40px;
  text-align: center;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.5);
}

.profile-header {
  margin-bottom: 30px;
}

.profile-avatar {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  margin: 0 auto 20px;
  overflow: hidden;
  border: 3px solid var(--primary);
}

.avatar-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.profile-name {
  font-size: 2.5rem;
  color: var(--dark);
  margin-bottom: 10px;
}

.profile-title {
  font-size: 1.2rem;
  color: var(--primary);
  margin-bottom: 15px;
}

.profile-description {
  color: var(--gray);
  font-size: 1.1rem;
  line-height: 1.6;
  max-width: 600px;
  margin: 0 auto;
}

.social-links {
  display: flex;
  justify-content: center;
  gap: 20px;
}

.social-link {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--card-bg);
  color: var(--dark);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  transition: var(--transition);
  border: 1px solid rgba(0, 0, 0, 0.1);
}

.social-link:hover {
  background: var(--primary-light);
  color: white;
  transform: translateY(-3px);
}

.featured-section {
  margin-top: 40px;
}

.section-title {
  font-size: 2rem;
  margin-bottom: 30px;
  position: relative;
  padding-bottom: 15px;
  color: var(--dark);
  padding-left: 15px;
}

.section-title:after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 15px;
  width: 60px;
  height: 4px;
  background: var(--primary);
  border-radius: 2px;
}

.articles-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 30px;
  padding: 0 15px;
}

.blog-card {
  background: var(--card-bg);
  border-radius: 24px;
  overflow: hidden;
  transition: var(--transition);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.5);
  cursor: pointer;
  position: relative;
}

.blog-card:hover {
  transform: translateY(-10px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
}

.blog-card::after {
  content: '点击查看文章';
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(0, 0, 0, 0.6);
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 0.7rem;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.blog-card:hover::after {
  opacity: 1;
}

.card-img {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.card-content {
  padding: 20px;
}

.card-tag {
  display: inline-block;
  background: var(--primary-light);
  color: white;
  padding: 5px 15px;
  border-radius: 50px;
  font-size: 0.8rem;
  margin-bottom: 15px;
}

.card-title {
  font-size: 1.4rem;
  margin-bottom: 12px;
  color: var(--dark);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.4;
}

.card-excerpt {
  color: var(--gray);
  margin-bottom: 20px;
  line-height: 1.6;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-meta {
  display: flex;
  justify-content: space-between;
  color: var(--gray);
  font-size: 0.9rem;
}

.card-meta i {
  margin-right: 5px;
}

/* 添加过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
}

@media (max-width: 900px) {
  .profile-card {
    padding: 30px;
  }

  .profile-avatar {
    width: 100px;
    height: 100px;
    font-size: 2.5rem;
  }

  .profile-name {
    font-size: 2rem;
  }

  .profile-title {
    font-size: 1.1rem;
  }

  .profile-description {
    font-size: 1rem;
  }

  .articles-grid {
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 20px;
  }

  .section-title {
    font-size: 1.8rem;
  }
}

@media (max-width: 600px) {
  .home-page {
    padding: 10px 0;
  }

  .profile-card {
    padding: 20px;
    border-radius: 0;
  }

  .profile-avatar {
    width: 80px;
    height: 80px;
    font-size: 2rem;
  }

  .profile-name {
    font-size: 1.8rem;
  }

  .profile-title {
    font-size: 1rem;
  }

  .profile-description {
    font-size: 0.95rem;
  }

  .social-links {
    gap: 15px;
  }

  .social-link {
    width: 35px;
    height: 35px;
    font-size: 1.1rem;
  }

  .articles-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
    padding: 0 5px;
  }

  .section-title {
    font-size: 1.5rem;
    margin-bottom: 20px;
    padding-left: 5px;
  }

  .section-title:after {
    left: 5px;
    width: 40px;
  }

  .blog-card {
    border-radius: 12px;
  }

  .blog-card::after {
    display: none;
  }

  .card-img {
    height: 120px;
  }

  .card-content {
    padding: 10px;
  }

  .card-tag {
    padding: 3px 8px;
    font-size: 0.7rem;
    margin-bottom: 8px;
  }

  .card-title {
    font-size: 0.9rem;
    margin-bottom: 6px;
  }

  .card-excerpt {
    font-size: 0.8rem;
    margin-bottom: 8px;
    -webkit-line-clamp: 2;
  }

  .card-meta {
    font-size: 0.7rem;
  }

  .card-meta i {
    margin-right: 3px;
  }
}
</style> 